<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/29
 * Time: 14:07
 */
namespace app\api\validate;

use app\lib\exception\ParemeterException;

class OrderPlace extends BaseValidate {

    protected $rule = [
        'products' => 'checkProducts'
    ];

    protected $singRule = [
        'product_id' => 'require|isPositveInteger',
        'count' => 'require|isPositveInteger'
    ];

    /**
     * @param $values
     * @return bool
     * @throws ParemeterException
     * @throws \think\Exception
     */
    protected function checkProducts($values){
        if(!is_array($values)){
            throw new ParemeterException(['msg'=>'商品参数不正确']);
        }
        if(empty($values)){
            throw  new ParemeterException(['msg'=>'商品列表不能为空']);
        }
        foreach ($values as $value) {
            $this->checkProductValue($value);
        }
        return true;
    }

    /**
     * @param $value
     * @throws ParemeterException
     * @throws \think\Exception
     */
    protected function checkProductValue($value){
        $validated = new BaseValidate($this->singRule);
        $result = $validated->check($value);
        if(!$result){
            throw new ParemeterException(['msg'=>'商品列表参数错误']);
        }
    }
}














